<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
       <el-form-item label="单价/SHJC" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入单价/SHJC"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="交易时间" prop="createDate">
         <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="数量排序" prop="orderNum">
        <el-select v-model="queryParams.orderNumSort" placeholder="请选择排序方式" clearable>
          <el-option label="升序" value="asc" />
          <el-option label="降序" value="desc" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <!-- <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['bill:billView:add']"
        >新增</el-button> -->
      </el-col>
      <el-col :span="1.5">
        <!-- <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['bill:billView:edit']"
          >修改</el-button
        > -->
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['bill:billView:remove']"
          >删除</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['bill:billView:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

       <div>
        <span>总账单次数：{{ totalForm.infoTotal }}</span></br>
        <span>总账单金额：{{ totalForm.balanceTotal}}</span></br>
        <span>总数量/张/张：{{ totalForm.cardTotal }}</span>
      </div>

    <el-table
      v-loading="loading"
      :data="billViewList"
      @selection-change="handleSelectionChange"
      @sort-change="handleSortChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="账单ID" align="center" prop="billId" /> -->
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="用户名" align="center" prop="userName" />
      <el-table-column label="订单号" align="center" prop="orderId" />
      <el-table-column label="商品名称" align="center" prop="shopName" />
      <el-table-column label="数量/张/张" align="center" prop="orderNum" sortable="custom" />
      <el-table-column label="总价/SHJC" align="center" prop="totalAmount" sortable="custom" />
      <el-table-column label="单价/SHJC" align="center" prop="price" sortable="custom" />
      <el-table-column
        label="交易时间"
        align="center"
        prop="createDate"
        width="180"
        sortable="custom"
      />
         <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['bill:billView:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改账单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="关联订单ID" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入关联订单ID" />
        </el-form-item>
        <el-form-item label="总价/SHJC" prop="totalAmount">
          <el-input v-model="form.totalAmount" placeholder="请输入总价/SHJC" />
        </el-form-item>
        <el-form-item label="手续费金额" prop="feeAmount">
          <el-input v-model="form.feeAmount" placeholder="请输入手续费金额" />
        </el-form-item>
        <el-form-item label="单价/SHJC" prop="price">
          <el-input v-model="form.price" placeholder="请输入手续费金额" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBillView,
  getBillView,
  delBillView,
  addBillView,
  updateBillView,
  totalBillInfoList
} from "@/api/bill/billView";

export default {
  name: "BillView",
  data() {
    return {
      dateRange: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 账单表格数据
      billViewList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        orderId: null,
        totalAmount: null,
        feeAmount: null,
        createDate: null,
        updatedAt: null,
        orderByColumn: null,
        isAsc: 'asc',
        orderNum: null,
        orderNumSort: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "用户ID不能为空", trigger: "blur" },
        ],
        orderId: [
          { required: true, message: "关联订单ID不能为空", trigger: "blur" },
        ],
        totalAmount: [
          { required: true, message: "总价/SHJC不能为空", trigger: "blur" },
        ],
        createDate: [
          { required: true, message: "创建时间不能为空", trigger: "blur" },
        ],
        updatedAt: [
          { required: true, message: "更新时间不能为空", trigger: "blur" },
        ],
      },
      totalForm: {
        infoTotal: 0,
        balanceTotal:0,
        cardTotal:0
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询账单列表 */
  async  getList() {
      this.loading = true;
      // 处理日期范围参数
      let params = {
        ...this.queryParams
      };
      if (this.dateRange && this.dateRange.length === 2) {
        params.startTime = this.dateRange[0];
        params.endTime = this.dateRange[1];
      }
      // 处理数量排序
      if (this.queryParams.orderNumSort) {
        params.orderByColumn = 'orderNum';
        params.isAsc = this.queryParams.orderNumSort;
      }
      listBillView(params).then((response) => {
        this.billViewList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      /** 统计数据 */
      const res = await totalBillInfoList(params);
      if (res.code == 200) { 
        this.totalForm = res.data;
      }

    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        billId: null,
        userId: null,
        orderId: null,
        totalAmount: null,
        feeAmount: null,
        createDate: null,
        updatedAt: null,
        orderNum: null,
        orderNumSort: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.queryParams.orderNumSort = null;
      this.queryParams.orderByColumn = null;
      this.queryParams.isAsc = 'asc';
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.billId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加账单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const billId = row.billId || this.ids;
      getBillView(billId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改账单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.billId != null) {
            updateBillView(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBillView(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const billIds = row.billId || this.ids;
      this.$modal
        .confirm('是否确认删除账单编号为"' + billIds + '"的数据项？')
        .then(function () {
          return delBillView(billIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      if (this.billViewList.length > 0) {
        // 处理日期范围参数
        let params = {
          ...this.queryParams
        };
        if (this.dateRange && this.dateRange.length === 2) {
          params.startTime = this.dateRange[0];
          params.endTime = this.dateRange[1];
        }
        this.download(
          "bill/billView/export",
          params,
          `账单记录表${new Date().getTime()}.xlsx`
        );
      } else { 
        this.$modal.msgError("未查询到数据");
      }
 
    },
    /** 排序触发事件 */
    handleSortChange(column) {
      if (column.prop) {
        this.queryParams.orderByColumn = column.prop;
        this.queryParams.isAsc = column.order === 'ascending' ? 'asc' : 'desc';
      } else {
        this.queryParams.orderByColumn = null;
        this.queryParams.isAsc = 'asc';
      }
      this.getList();
    },
  },
};
</script>
